﻿/*
Copyright (c) 2010 Vratislav Cermak (vratacermak@gmail.com)
 
This file is part of TeachMe Project. See http://code.google.com/p/teach-me for more information.
 
TeachMe is an open source project. Using of TeachMe is free of charge.
It is distributed under GNU General Public License version 3 (GPLv3).
GPLv3 can be found at http://www.gnu.org/licenses/gpl.html.
 
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

using System;
using System.IO;
using System.Xml.Serialization;

namespace TeachMe.Application.Configuration
{
    public class ImportExcludes : Checkable
    {
        private string _path;

        private Property<string> _words;

        public Property<string> Words
        {
            get
            {
                return _words ?? (_words = new Property<string>(this, "a; the; an; and; to; from; with; or; me; you"));
            }
            set
            {
                _words = value;
            }
        }

        public string[] GetParsedWords()
        {
            return String.IsNullOrEmpty(Words.Value) ? new string[]{} : Words.Value.Split(new[] {';', ' '}, StringSplitOptions.RemoveEmptyEntries);
        }

        public static ImportExcludes Load(string path)
        {
            ImportExcludes importExcludes = null;

            if (File.Exists(path))
            {
                Logging.Debug(typeof(ImportExcludes), "Loading import excludes from '{0}'.", path);

                try
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(ImportExcludes));
                    using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read))
                    {
                        importExcludes = serializer.Deserialize(stream) as ImportExcludes;
                    }
                }
                catch (Exception e)
                {
                    Logging.Error(typeof(ImportExcludes), e, "Import excludes could not be read.", path);
                }
            }
            if (importExcludes == null)
            {
                Logging.Debug(typeof(ImportExcludes), "Default import excludes will be created.", path);

                importExcludes = new ImportExcludes();
            }

            importExcludes._path = path;

            return importExcludes;
        }

        protected override void Save()
        {
            try
            {
                Logging.Debug(GetType(), "Saving import excludes to '{0}'.", _path);

                XmlSerializer serializer = new XmlSerializer(typeof(ImportExcludes));
                using (FileStream stream = new FileStream(_path, FileMode.Create, FileAccess.Write))
                {
                    serializer.Serialize(stream, this);
                }
            }
            catch (Exception e)
            {
                Logging.Error(GetType(), e, "Import excludes could not be saved to '{0}'.", _path);
            }
        }
    }
}
